package web;

import bean.Items;
import bean.Users;
import service.Impl.UserServiceImpl;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 栋
 * @date 2022/11/27
 * @description
 */
@WebServlet("/itemServlet")
public class ItemServlet extends baseServlet{
    UserService userService=new UserServiceImpl();

    /**
     * 显示商品具体信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showItemInfor(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String item_id = req.getParameter("item_id");
        Items i = userService.showItemInfor(item_id);
        if(i!=null){
            req.getSession().setAttribute("i",i);
            req.getRequestDispatcher("/pages/items/itemInfor.jsp").forward(req, resp);
        }
    }

    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String item_id = req.getParameter("item_id");
        Users user = (Users)req.getSession().getAttribute("user");
        userService.deleteItem(item_id);
        List<Items> personalItems = userService.showPersonalItems(user.getUser_id());
        req.setAttribute("msg","删除成功");
        req.getSession().setAttribute("personalItems",personalItems);
        req.getRequestDispatcher("pages/items/personalItems.jsp").forward(req, resp);
    }


}
